#include <stdio.h>
#include <string.h>
#include "sqlite3.h"

int wf_callback(void *arg, int nr, char **values, char **name)
{
    int i;
    FILE *fd;
    char str[128];
    fd = (FILE *)arg;
    for (i = 0; i < nr; i++)
    {
        memset(str, '\0', 128);
        sprintf(str, "\t%s\t", values[i]);
        fwrite(str, sizeof(char), sizeof(str), fd);
    }
    memset(str, '\0', 128);
    fwrite("\n", sizeof(char), 2, fd);
    return 0;
}

int main(int argc, char *argv[])
{
    char sql[128];
    sqlite3 *db;
    FILE *fd;

    sqlite3_open("test.db", &db);//open database

    // create a new table
    memset(sql, '\0', 128);
    strcpy(sql, "create table tb(id INTEGER PRIMARY KEY, name TEXT)");
    sqlite3_exec(db, sql, NULL, NULL, NULL);

    // execute select command and write result to file.
    fd = fopen("testdb.txt", "w");
    fwrite("Result: \n", sizeof(char), 10, fd);
    memset(sql, '\0', 128);
    strcpy(sql, "insert into tb(id, name) values(1, 'czx')");
    sqlite3_exec(db, sql, NULL, NULL, NULL);
    memset(sql, '\0', 128);
    strcpy(sql, "select * from tb");
    sqlite3_exec(db, sql, wf_callback, fd, NULL);
    fclose(fd);

    sqlite3_close(db);//close database

    return 0;
}
